package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加菜品或套餐进入购物车
     *
     * @param shoppingCartDTO
     */
    @Transactional
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {


        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);


        /**
         * 有没有在购物车中出现（在购物车中出现了）
         */
        if (list != null && !list.isEmpty()) {
            ShoppingCart shoppingCart0 = list.get(0);
            Integer number = shoppingCart0.getNumber();
            shoppingCart0.setNumber(number+1);
            shoppingCartMapper.update(shoppingCart0);
        } else {
            /**
             * 填充参数
             */
            if (shoppingCart.getDishId() != null) {
                DishVO dishVO = dishMapper.findById(Math.toIntExact(shoppingCart.getDishId()));
                shoppingCart = ShoppingCart.builder()
                        .image(dishVO.getImage())
                        .userId(currentId)
                        .dishId(dishVO.getId())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .amount(dishVO.getPrice())
                        .createTime(LocalDateTime.now())
                        .name(dishVO.getName())
                        .build();
            } else {
                Setmeal setmeal = setmealMapper.findById(Math.toIntExact(shoppingCart.getSetmealId()));
                shoppingCart = ShoppingCart.builder()
                        .image(setmeal.getImage())
                        .userId(currentId)
                        .setmealId(setmeal.getId())
                        .amount(setmeal.getPrice())
                        .createTime(LocalDateTime.now())
                        .name(setmeal.getName())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .build();
            }
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart build = ShoppingCart.builder().userId(currentId).build();
        List<ShoppingCart> list = shoppingCartMapper.list(build);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
